import loadingMixin from './loadingMixin';

export default {
    mixins: [loadingMixin],
    data: function () {
        return {
            extraConfig: {},
            dataList: [],
            totalDataList: [],
            queryForm: {
                pageNo: 1,
                pageSize: 10,
                total: 0,
            },
            paging: {
                background: true,
                sizes: [10, 20, 30, 50, 100],
                layout: ' prev, pager, next, sizes, jumper',
            },
        };
    },
    methods: {
        /* 异步页码切换事件 */
        pageIndexChange(val) {
            this.queryForm.pageNo = val;
            this.query();
        },
        /* 异步分页数量切换事件 */
        pageSizeChange(pageSize) {
            this.queryForm.pageNo = 1;
            this.queryForm.pageSize = pageSize;
            // localStorage.setItem('paging.pageSize', this.queryForm.pageSize)
            this.query();
        },
        reFilter() {
            this.extraConfig = {};
            this.queryForm.pageNo = 1;
            this.query();
        },
        /* 接口请求，使用queryImpl */
        query(param) {
            this.setLoading();
            this.queryImpl(param)
                .then(() => {
                    this.resetLoading();
                })
                .catch(() => {
                    this.resetLoading();
                });
        },
        setResult(list, total) {
            this.dataList = list;
            this.queryForm.total = Number(total);
        },
        /* 前端分页事件 */
        /* 异步页码切换事件 */
        forPageIndexChange(val) {
            this.queryForm.paging.pageNo = val;
            this.dataList = this.getForDataList();
        },
        /* 分页数量切换事件 */
        forPageSizeChange(pageSize) {
            this.queryForm.paging.pageNo = 1;
            this.queryForm.paging.pageSize = pageSize;
            this.dataList = this.getForDataList();
        },
        forSetResult(list, total) {
            this.totalDataList = list;
            if (!total) {
                total = list ? list.length : 0;
            }
            this.queryForm.paging.total = total;
            this.dataList = this.getForDataList();
        },
        getForDataList() {
            if (this.totalDataList.length > 0) {
                const pageSize = this.queryForm.paging.pageSize;
                const pageNo = this.queryForm.paging.pageNo;
                return this.totalDataList.slice((pageNo - 1) * pageSize, pageNo * pageSize);
            }
            return [];
        },
    },
};
